<?php
class it_menu_model extends MY_Model {
	var $model = 'itmenu';
	
	function GetTreeData(){
		$ls_sql	= "	SELECT	MENU_ID, MENU_LABEL, ICON, URL, ORDER_NUM, STATUS,
							CREATE_USER, CREATE_DATE, MODIFY_USER, MODIFY_DATE, 
							(SELECT COUNT(1) FROM IT_MENU TMP WHERE TMP.PARENT_MENU_ID = D.MENU_ID) NUMCHILD
                    FROM	IT_MENU D
                    WHERE	PARENT_MENU_ID = '0'
							AND APP_ID = '".APP."'
                    ORDER BY ORDER_NUM";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				if($arr_data[$i]['icon'] != ''){
					$arr_data[$i]['iconCls']	= 'icon-' . $arr_data[$i]['icon'];
				}
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_data[$i]['children']	= $this->GetTreeSubData($arr_data[$i]['menu_id']);
					$arr_data[$i]['state']		= "closed";
				}
			}
			
			return $arr_data;
		}else{
			return NULL;
		}
	}
	
	function GetTreeSubData($parent_id=''){
		if($parent_id=='') return NULL;
		$ls_sql	= "	SELECT	MENU_ID, MENU_LABEL, ICON, URL, ORDER_NUM, STATUS,
							CREATE_USER, CREATE_DATE, MODIFY_USER, MODIFY_DATE, 
							(SELECT COUNT(1) FROM IT_MENU TMP WHERE TMP.PARENT_MENU_ID = D.MENU_ID) NUMCHILD
                    FROM	IT_MENU D
                    WHERE	PARENT_MENU_ID = '".$parent_id."'
							AND APP_ID = '".APP."'
                    ORDER BY ORDER_NUM";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				if($arr_data[$i]['icon'] != ''){
					$arr_data[$i]['iconCls']	= 'icon-' . $arr_data[$i]['icon'];
				}
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_data[$i]['children']	= $this->GetTreeSubData($arr_data[$i]['menu_id']);
					$arr_data[$i]['state']		= "closed";
				}
			}
			
			return $arr_data;
		}else{
			return NULL;
		}
	}
		
	function Add($arr_data){
		$this->db->insert('it_menu', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('it_menu', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_menuid){
		$result = $this->db->get_where('it_menu', array('menu_id' => $as_menuid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_menuid)
	{
		$ls_sql	= "	SELECT	MENU_ID, PARENT_MENU_ID, MENU_LABEL, ICON, URL, ORDER_NUM, 
							CREATE_USER, CREATE_DATE, MODIFY_USER, MODIFY_DATE
                    FROM	IT_MENU D
                    WHERE	MENU_ID = '".$as_menuid."'";
							
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetFirstLevelMenuByRole(){
		$result = $this->db->query("SELECT	A.MENU_ID, A.MENU_LABEL, A.URL, A.ICON, 
											(SELECT COUNT(1) FROM IT_MENU TMP WHERE TMP.PARENT_MENU_ID = A.MENU_ID) NUMCHILD
                                    FROM    IT_MENU A 
                                            JOIN IT_MENU_ACCESS B ON A.MENU_ID = B.MENU_ID 
                                            JOIN IT_USER_ROLE C ON B.ROLE_ID = C.ROLE_ID 
                                    WHERE   A.PARENT_MENU_ID = '0' 
											AND A.APP_ID = (SELECT X.APP_ID FROM IT_SESSION X WHERE X.SESSID = '"._SESSID."') 
                                            AND C.ROLE_ID = (SELECT X.ROLE_ID FROM IT_SESSION X WHERE X.SESSID = '"._SESSID."') 
                                            AND C.USER_ID = '" . USERID . "' 
                                    ORDER BY A.ORDER_NUM");
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return NULL;
		}
	}
	
	function GetChildMenuByRole($as_menuid){
		$result = $this->db->query("SELECT	A.MENU_ID ID, A.MENU_LABEL TEXT, A.URL, A.ICON,
											(SELECT COUNT(1) FROM IT_MENU TMP WHERE TMP.PARENT_MENU_ID = A.MENU_ID) NUMCHILD
                                    FROM    IT_MENU A 
                                            JOIN IT_MENU_ACCESS B ON A.MENU_ID = B.MENU_ID 
                                            JOIN IT_USER_ROLE C ON B.ROLE_ID = C.ROLE_ID 
									WHERE    A.PARENT_MENU_ID = '".$as_menuid."' 
                                            AND A.APP_ID = (SELECT X.APP_ID FROM IT_SESSION X WHERE X.SESSID = '"._SESSID."') 
											AND C.ROLE_ID = (SELECT X.ROLE_ID FROM IT_SESSION X WHERE X.SESSID = '"._SESSID."') 
                                            AND C.USER_ID = '" . USERID . "' 
                                    ORDER BY A.ORDER_NUM");
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				if($arr_data[$i]['icon'] != ''){
					$arr_data[$i]['iconCls']	= 'icon-' . $arr_data[$i]['icon'];
				}
				unset($arr_data[$i]['icon']);
				
				if(strlen($arr_data[$i]['url']) > 2 && @strstr($arr_data[$i]['url'],'/')!=''){
					$arr_data[$i]['attributes']['url']	= $arr_data[$i]['url'];
					$arr_data[$i]['attributes']['menu_id']	= $arr_data[$i]['id'];
				}
				unset($arr_data[$i]['url']);
				
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_data[$i]['children']	= $this->GetChildMenuByRole($arr_data[$i]['id']);
					$arr_data[$i]['state']		= "closed";
				}
			}
			
			return $arr_data;
		}else{
			return NULL;
		}
	}
	
	function GetChild($as_menuid){
		$result = $this->db->get_where('it_menu', array('parent_menu_id' => $as_menuid));
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return NULL;
		}
	}
	
	function DeleteSingle($as_menuid)
	{
		try {
			$this->db->delete('it_menu_access', array('menu_id' => $as_menuid));
			$this->db->delete('it_menu', array('menu_id' => $as_menuid));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
	
	function GetComboTreeData(){
		$ls_sql	= "	SELECT	MENU_ID ID, MENU_LABEL TEXT, (SELECT COUNT(1) FROM IT_MENU TMP WHERE TMP.PARENT_MENU_ID = D.MENU_ID) NUMCHILD
                    FROM	IT_MENU D
                    WHERE	PARENT_MENU_ID = '0'
							AND APP_ID = '".APP."'
                    ORDER BY D.ORDER_NUM";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_data[$i]['children']	= $this->GetComboTreeSubData($arr_data[$i]['id']);
				}
			}
			
			return array(array('id' => '0', 'text' => '', 'children' => $arr_data));
		}else{
			return NULL;
		}
	}
	
	function GetComboTreeSubData($parent_id=''){
		if($parent_id=='') return NULL;
		
		$ls_sql	= "	SELECT	MENU_ID ID, MENU_LABEL TEXT, (SELECT COUNT(1) FROM IT_MENU TMP WHERE TMP.PARENT_MENU_ID = D.MENU_ID) NUMCHILD
                    FROM	IT_MENU D
                    WHERE	PARENT_MENU_ID = '".$parent_id."'
							AND APP_ID = '".APP."'
                    ORDER BY D.ORDER_NUM";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_data[$i]['children'] = $this->GetComboTreeSubData($arr_data[$i]['id']);
				}
			}
			
			return $arr_data;
		}else{
			return NULL;
		}
	}
}
?>